﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ext.Net;
using DemoExtNet.BusinessObject.BusinessEntities;

namespace DemoExtNet.PhanQuyen.UserControls
{
    public partial class QUNhomQuyenByToChuc_Detail : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
            }
        }

        protected int NhomQuyenByToChucId
        {
            get {
                return int.Parse(this.__NhomQuyenByToChucId.Text);
            }
            set {
                this.__NhomQuyenByToChucId.Text = value.ToString();
            }
        }

        public void ShowDialog(int pNhomQuyenByToChucId)
        {
            //Save Id
            this.NhomQuyenByToChucId = pNhomQuyenByToChucId;
            //Get data
            var oQUNhomQuyenByToChuc = new QUNhomQuyenByToChuc();
            oQUNhomQuyenByToChuc = oQUNhomQuyenByToChuc.GetById(pNhomQuyenByToChucId);
            //Bind data into Detail Area
            this.lblTenNhomQuyen.Text = oQUNhomQuyenByToChuc.TenNhomQuyenByToChuc;
            this.lblGhiChu.Text = oQUNhomQuyenByToChuc.GhiChu;
            //Bind data into NguoiDung Area
            UpdateGridNguoiDung();
            //Bind data into ToChuc Area
            BuildTreeToChuc();
            //Show Dialog
            this.wdQUNhomQuyenByToChuc_Detail.Show();
        }

        public void UpdateGridNguoiDung()
        {
            var oQUNguoiDung = new BusinessObject.BusinessEntities.QUNguoiDung();
            storeNguoiDung.DataSource = oQUNguoiDung.GetBy_NguoiDungByToChucId(NhomQuyenByToChucId);
            storeNguoiDung.DataBind();
        }

        protected void SubmitNodes(object sender, SubmitEventArgs e)
        {
            var lstItemNeedToSave = new List<QUNhomQuyenByToChuc_ToChuc>();
            ReadNode(e.RootNode, lstItemNeedToSave);
            var oQUNhomQuyenByToChuc_ToChuc = new QUNhomQuyenByToChuc_ToChuc();
            oQUNhomQuyenByToChuc_ToChuc.SaveData(this.NhomQuyenByToChucId, lstItemNeedToSave);
            btnSaveTree.Disabled = true;

        }

        private void ReadNode(SubmittedNode Node, List<QUNhomQuyenByToChuc_ToChuc> lstItems)
        {
          
            if (Node.Attributes.Count > 0)
            {
                QUNhomQuyenByToChuc_ToChuc item = new QUNhomQuyenByToChuc_ToChuc();
                item.NhomQuyenByToChucID = this.NhomQuyenByToChucId;
                item.ToChucID = int.Parse(Node.Text);
                item.IsView = bool.Parse(Node.Attributes["IsView"].ToString());
                item.IsAdd = bool.Parse(Node.Attributes["IsAdd"].ToString());
                item.IsEdit = bool.Parse(Node.Attributes["IsEdit"].ToString());
                item.IsDelete = bool.Parse(Node.Attributes["IsDelete"].ToString());
                item.IsModuleConThuaKe = bool.Parse(Node.Attributes["IsModuleConThuaKe"].ToString());
                if (item.IsView || item.IsAdd || item.IsEdit || item.IsDelete || item.IsModuleConThuaKe)
                {
                    lstItems.Add(item);
                }
                
            }

            if (Node.Children.Count > 0)
            {
                foreach (var oItem in Node.Children)
                {
                    ReadNode(oItem, lstItems);
                }
            }
        }

        protected void btnShowDialogAddNguoiDung_Click(object sender, DirectEventArgs e)
        {
            ((PagePhanQuyenByToChuc)this.Page).ShowDialog_QUNguoiDung(NhomQuyenByToChucId);
        }

        private void BuildTreeToChuc()
        {
            List<NSToChuc> lstToChuc = new List<NSToChuc>();
            lstToChuc = (new NSToChuc()).GetAll();

            var lstToChucLv1 = (from oToChuc in lstToChuc
                           where oToChuc.ToChucLevel == 1
                           select oToChuc).ToList();
           
            TreeNode nodeToChuc = new TreeNode("ToChuc");
            TreeGrid1.Root.Add(nodeToChuc);
            
            foreach (var oToChucLv1 in lstToChucLv1)
            { 
                //Create Node
                var nodeLv1 = CreateNode(oToChucLv1);
                nodeToChuc.Nodes.Add(nodeLv1);

                var lstToChucLv2 = (from oToChuc in lstToChuc
                                    where oToChuc.ToChucLevel == 2 && oToChuc.ToChucIDCha == oToChucLv1.ToChucID
                                    select oToChuc).ToList();
                if (lstToChucLv2.Count == 0)
                {
                    nodeLv1.Leaf = true;
                    nodeLv1.Icon = Icon.None;
                }
                else
                {
                    nodeLv1.Icon = Icon.Folder;
                }

                foreach (var oToChucLv2 in lstToChucLv2)
                {
                    var nodeLv2 = CreateNode(oToChucLv2);
                    nodeLv1.Nodes.Add(nodeLv2);

                    var lstToChucLv3 = (from oToChuc in lstToChuc
                                        where oToChuc.ToChucLevel == 3 && oToChuc.ToChucIDCha==oToChucLv2.ToChucID
                                        select oToChuc).ToList();

                    if (lstToChucLv3.Count == 0)
                    {
                        nodeLv2.Leaf = true;
                        nodeLv2.Icon = Icon.None;
                    }
                    else {
                        nodeLv2.Icon = Icon.Folder;
                    }
                    foreach (var oToChucLv3 in lstToChucLv3)
                    {
                        nodeLv2.Nodes.Add(CreateNode(oToChucLv3));
                    }
                }
            }
            //Render to client
            Ext.Net.X.AddScript(string.Format("refreshTree({0},{1});",TreeGrid1.ClientID, TreeGrid1.Root.ToJson()));
        }

        private TreeNode CreateNode(NSToChuc oNSToChuc)
        {          

            TreeNode oNode = new TreeNode(oNSToChuc.ToChucID.ToString(), Icon.None);
            //Bind data to "Is View...
            var oQUNhomQuyenByToChuc_ToChuc = (from item in ListQUNhomQuyenByToChuc_ToChuc
                                               where item.ToChucID == oNSToChuc.ToChucID
                                               select item).FirstOrDefault();
            var IsView = false;
            var IsAdd = false;
            var IsEdit = false;
            var IsDelete = false;
            var IsModuleConThuaKe = false;

            if (oQUNhomQuyenByToChuc_ToChuc != null)
            {
                IsView = oQUNhomQuyenByToChuc_ToChuc.IsView;
                IsAdd = oQUNhomQuyenByToChuc_ToChuc.IsAdd;
                IsEdit = oQUNhomQuyenByToChuc_ToChuc.IsEdit;
                IsDelete = oQUNhomQuyenByToChuc_ToChuc.IsDelete;
                IsModuleConThuaKe = oQUNhomQuyenByToChuc_ToChuc.IsModuleConThuaKe;
            }

            oNode.CustomAttributes.Add(new ConfigItem("TenToChuc", oNSToChuc.TenToChucShort, ParameterMode.Value));
            oNode.CustomAttributes.Add(new ConfigItem("IsView", IsView.ToString().ToLower(), ParameterMode.Value));
            oNode.CustomAttributes.Add(new ConfigItem("IsAdd", IsAdd.ToString().ToLower(), ParameterMode.Value));
            oNode.CustomAttributes.Add(new ConfigItem("IsEdit", IsEdit.ToString().ToLower(), ParameterMode.Value));
            oNode.CustomAttributes.Add(new ConfigItem("IsDelete", IsDelete.ToString().ToLower(), ParameterMode.Value));
            oNode.CustomAttributes.Add(new ConfigItem("IsModuleConThuaKe", IsModuleConThuaKe.ToString().ToLower(), ParameterMode.Value));
            return oNode;
        }

        private List<QUNhomQuyenByToChuc_ToChuc> _lstQUNhomQuyenByToChuc_ToChuc;

        public List<QUNhomQuyenByToChuc_ToChuc> ListQUNhomQuyenByToChuc_ToChuc
        {
            get {
                if (_lstQUNhomQuyenByToChuc_ToChuc == null)
                { 
                    _lstQUNhomQuyenByToChuc_ToChuc=(new QUNhomQuyenByToChuc_ToChuc()).GetByNhomQuyenByToChucId(this.NhomQuyenByToChucId);

                }
                return _lstQUNhomQuyenByToChuc_ToChuc;
            }
        }

        [DirectMethod]
        public void RemoveNguoiDung(int pNguoiDungId)
        {
            var oQUNhomQuyenByToChuc_NguoiDung = new QUNhomQuyenByToChuc_NguoiDung();
            oQUNhomQuyenByToChuc_NguoiDung.NguoiDungID = pNguoiDungId;
            oQUNhomQuyenByToChuc_NguoiDung.NhomQuyenByToChucID = this.NhomQuyenByToChucId;
            oQUNhomQuyenByToChuc_NguoiDung.Delete();
            UpdateGridNguoiDung();
        }

    }
}